from bs4 import BeautifulSoup
import requests
from requests.api import patch
baseurl = 'https://www.h6pkq.com'
url = 'https://www.h6pkq.com/tupian/list-自拍偷拍-'

file = r'D:\Deng\Desktop\a.txt'

for i in range(51,253):
    res = requests.get(url+str(i))
    res.encoding=('utf-8')
    html = res.text
    soup = BeautifulSoup(html, 'lxml')
    div = soup.find(id='tpl-img-content')
    divList = div.find_all('li')
    for j in divList:
        href = baseurl+j.a['href']
        title = j.a['title']
        print(href+' : '+title)
        try:
             with open(file, 'a+',encoding='gbk') as f:
                f.write(href+' : '+title+'\n')   #加\n换行显示
        except Exception:
            pass
    print("第"+str(i)+'次存储完成')
